
/****************************************************************************/
/*Copyright (c) 2011, Florent DEVILLE.                                      */
/*All rights reserved.                                                      */
/*                                                                          */
/*Redistribution and use in source and binary forms, with or without        */
/*modification, are permitted provided that the following conditions        */
/*are met:                                                                  */
/*                                                                          */
/* - Redistributions of source code must retain the above copyright         */
/*notice, this list of conditions and the following disclaimer.             */
/* - Redistributions in binary form must reproduce the above                */
/*copyright notice, this list of conditions and the following               */
/*disclaimer in the documentation and/or other materials provided           */
/*with the distribution.                                                    */
/* - The names of its contributors cannot be used to endorse or promote     */
/*products derived from this software without specific prior written        */
/*permission.                                                               */
/* - The source code cannot be used for commercial purposes without         */ 
/*its contributors' permission.                                             */
/*                                                                          */
/*THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS       */
/*"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT         */
/*LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS         */
/*FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE            */
/*COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,       */
/*INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,      */
/*BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;          */
/*LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER          */
/*CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT        */
/*LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN         */
/*ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE           */
/*POSSIBILITY OF SUCH DAMAGE.                                               */
/****************************************************************************/


#pragma once

#include <math.h>
#include <iostream>
using std::cout;
#include "RTMatrix44.h"

#include "RTVector4f.h"

namespace RT
{

	//3 floats vector class
	class RTVector3f
	{
	public:
		F32 x, y, z;

	public:
		RTVector3f():x(0), y(0), z(0){};
		RTVector3f(F32 x_, F32 y_, F32 z_):x(x_), y(y_), z(z_){};

		virtual ~RTVector3f(){};


		RTVector3f operator +(const RTVector3f& v)const;
		RTVector3f operator -(const RTVector3f& v)const;
		RTVector3f operator -()const;
		RTVector3f operator *(F32 a)const;
		RTVector3f operator *(const RTVector3f& v)const;
		RTVector3f operator *(const RTMatrix44& M)const;
		RTVector3f operator /(F32 a)const;
		void operator += (const RTVector3f& v);
		void operator *= (F32 f);
		bool operator == (const RTVector3f& v);
		bool operator != (const RTVector3f& v);

		F32 dot(const RTVector3f& v)const;
		RTVector3f cross(const RTVector3f& v)const;

		F32 norme()const;
		F32 norme2()const;

		void normalize();

		void saturate();
		void maximize(F32 max);

		void display()const;

		void setByID(I32 id, F32 value);

		//convert into a vector4f
		//RTVector4f toVector4f(F32 w)const;
	};

	//add two vectors
	inline RTVector3f RTVector3f::operator +(const RTVector3f& v)const
	{
		RTVector3f result(x + v.x, y + v.y, z + v.z);
		return result;
	}

	//substraction of two vectors
	inline RTVector3f RTVector3f::operator -(const RTVector3f& v)const
	{
		RTVector3f result(x - v.x, y - v.y, z - v.z);
		return result;
	}

	//unary minus operator
	inline RTVector3f RTVector3f::operator -()const
	{
		return RTVector3f(-x, -y, -z);
	}

	//multiply a vector with a F32
	inline RTVector3f RTVector3f::operator *(F32 a)const
	{
		RTVector3f result(x * a, y * a, z * a);
		return result;
	}

	//multiply two vectors
	inline RTVector3f RTVector3f::operator *(const RTVector3f& v)const
	{
		RTVector3f result(x*v.x, y*v.y, z*v.z);
		return result;
	}

	//multiply a vector with a matrix
	inline RTVector3f RTVector3f::operator *(const RTMatrix44& M)const
	{
		RTVector3f res;

		res.x = x * M.m_array[0][0] + y * M.m_array[1][0] + z * M.m_array[2][0] + M.m_array[3][0];
		res.y = x * M.m_array[0][1] + y * M.m_array[1][1] + z * M.m_array[2][1] + M.m_array[3][1];
		res.z = x * M.m_array[0][2] + y * M.m_array[1][2] + z * M.m_array[2][2] + M.m_array[3][2];

		return res;
	}

	//divide a vector by a F32
	inline RTVector3f RTVector3f::operator /(F32 a)const
	{
		a = 1.f/a;

		return RTVector3f(x * a,	y * a, z * a);
	}

	//add and assign a vector
	inline void RTVector3f::operator += (const RTVector3f& v)
	{
		x+=v.x;
		y+=v.y;
		z+=v.z;
	}

	//multiply by a F32 and assign
	inline void RTVector3f::operator *= (F32 f)
	{
		x *= f;
		y *= f;
		z *= f;
	}

	//equal test
	inline bool RTVector3f::operator == (const RTVector3f& v)
	{
		if(x == v.x && y == v.y && z == v.z)
			return true;

		return false;
	}

	//unequal test
	inline bool RTVector3f::operator != (const RTVector3f& v)
	{
		if(x!=v.x || y!=v.y || z!=v.z)
			return true;

		return false;
	}

	//return the dot product of two vectors
	inline F32 RTVector3f::dot(const RTVector3f& v)const
	{
		return x*v.x + y*v.y + z*v.z;
	}

	//return the cross product of two vectors
	inline RTVector3f RTVector3f::cross(const RTVector3f& v)const
	{
		RTVector3f result(
			y*v.z - z*v.y,
			z*v.x - x*v.z,
			x*v.y - y*v.x);
		return result;

	}

	//return the norme of a vector
	inline F32 RTVector3f::norme()const
	{
		return sqrt(x*x + y*y + z*z);
	}

	//return the quare of the norme
	inline F32 RTVector3f::norme2()const
	{
		return x*x + y*y + z*z;
	}

	//normalize the vector
	inline void RTVector3f::normalize()
	{
		F32 n = norme();
		if(n == 0) return;
		x /= n;
		y /= n;
		z /= n;
	}

	//saturation between 0 and 1
	inline void RTVector3f::saturate()
	{
		if(x<0) x = 0;
		else if(x>1) x = 1;

		if(y<0) y = 0;
		else if(y>1) y = 1;

		if(z<0) z = 0;
		else if(z>1) z = 1;
	}

	//set the vector to a sepcific length (the vector needs to be normalized)
	inline void RTVector3f::maximize(F32 max)
	{
		x *= max;
		y *= max;
		z *= max;
	}

	//display the vector using cout
	inline void RTVector3f::display()const
	{
		cout<<x<<" "<<y<<" "<<z;
	}

	//set a component ofthe vector using an id (0 = x, 1 = y, z = 2)
	inline void RTVector3f::setByID(I32 id, F32 value)
	{
		switch(id)
		{
			case 0:
				x = value;
				break;

			case 1:
				y = value;
				break;

			case 2:
				z = value;
				break;

			default:
				break;
		}
	}

	////convert into a vector4f
	//inline RTVector4f RTVector3f::toVector4f(F32 w)const
	//{
	//	return RTVector4f(x, y, z, w);
	//}

}